package com.example.demo.leetcode.classics150;

import com.example.demo.leetcode.top100._41二叉树的层序遍历;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _74填充每个节点的下一个右侧节点指针II
 * @description
 * @date 2023/08/01 17:33
 * <p>
 * ******************************************************
 */
public class _74填充每个节点的下一个右侧节点指针II {


    public Node connect(Node root) {
        if (root == null) {
            return null;
        }
        LinkedList<Node> queue = new LinkedList<>();

        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Node> item = new LinkedList<>();
            List<Node> itemQueue = new LinkedList<>();
            final Iterator<Node> iterator = queue.iterator();
            while (iterator.hasNext()) {
                Node node = iterator.next();
                item.add(node);
                if (node.left != null) {
                    itemQueue.add(node.left);
                }
                if (node.right != null) {
                    itemQueue.add(node.right);
                }
                iterator.remove();
            }
            Node pre = null;
            for (Node node : itemQueue) {
                if (pre != null) {
                    pre.next = node;
                }
                queue.offer(node);
                pre = node;
            }
        }
        return root;
    }

    public Node connect1(Node root) {
        List<List<Node>> lists = levelOrder(root);
        for (List<Node> list : lists) {
            for (int i = 0; i < list.size() - 1; i++) {
                list.get(i).next = list.get(i + 1);
            }
        }
        return root;
    }

    public List<List<Node>> levelOrder(Node root) {
        List<List<Node>> result = new LinkedList<>();
        if (root == null) {
            return result;
        }
        LinkedList<Node> queue = new LinkedList<>();

        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Node> item = new LinkedList<>();
            List<Node> itemQueue = new LinkedList<>();
            final Iterator<Node> iterator = queue.iterator();
            while (iterator.hasNext()) {
                Node node = iterator.next();
                item.add(node);
                if (node.left != null) {
                    itemQueue.add(node.left);
                }
                if (node.right != null) {
                    itemQueue.add(node.right);
                }
                iterator.remove();
            }
            result.add(item);
            for (Node node : itemQueue) {
                queue.offer(node);
            }


        }
        return result;
    }

    class Node {
        public int val;
        public Node left;
        public Node right;
        public Node next;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, Node _left, Node _right, Node _next) {
            val = _val;
            left = _left;
            right = _right;
            next = _next;
        }
    }
}
